package com.proyecto.track.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.proyecto.track.bo.Period;
import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.Task;
import com.proyecto.track.bo.TaskStatus;
import com.proyecto.track.bo.UserPerson;

public class TaskDAOImp implements TaskDAO{

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public Long createTask(Task task) {
		sessionFactory.getCurrentSession().save(task);
		return task.getId();
	}

	@Override
	public Long deleteTask(Task task) {
		Long id = task.getId();
		sessionFactory.getCurrentSession().delete(task);
		return id;
	}

	@Override
	public Long modifyTask(Task task) {
		sessionFactory.getCurrentSession().update(task);
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Task> searchTask() {
		return  ( ArrayList<Task>) sessionFactory.getCurrentSession().createQuery("from Task").list();
	}

	@Override
	public Task searchTask(Long id) {
		return (Task) sessionFactory.getCurrentSession().get(Task.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Task> searchTask(Project project, UserPerson userPerson,
			Period period, TaskStatus taskStatus) {
		
		String newQuery = "select t from Project as p join p.tasks as t where p.id =:idp";

		if(userPerson.getId() != null){
			if(userPerson.getId() != -1){
				newQuery = "select t from Project as p join p.tasks as t join t.userTasks as ut where p.id =:idp";
				newQuery = newQuery.concat(" and ut.userPerson.id =:up");
				System.out.println(newQuery);
			}
		}
		
		if(taskStatus.getId() != null){
			if(taskStatus.getId() != -1){
				newQuery = newQuery.concat(" and t.taskStatus.id =:tStatusId");
				System.out.println(newQuery);
			}
			
		}
		
		if(period.getStartDate() != null){
			newQuery = newQuery.concat(" and t.period.startDate >= :sd");	
			System.out.println(newQuery);
				}
		
		if(period.getEndDate() != null){
			newQuery = newQuery.concat(" and t.period.endDate <= :ed");
			System.out.println(newQuery);
		}
		

		System.out.println(newQuery);
		System.out.println("select t from Project as p join p.tasks as t join t.userTasks as ut where p.id =:idp and t.taskStatus.id =:tStatusId and t.period.startDate >= :sd and t.period.endDate <= :ed" +
				" and ut.userPerson.id =:up");
		System.out.println(project.getId() + " " + taskStatus.getId() + " " + period.getStartDate() + " " + period.getEndDate());
		Query query = sessionFactory.getCurrentSession().createQuery(newQuery);

		/*"" +
				"select t from Project as p join p.tasks as t join t.userTasks as ut where p.id =:idp and t.taskStatus.id =:tStatusId and t.period.startDate >= :sd and t.period.endDate <= :ed" +
				" and ut.userPerson.id =:up"*/
				
		query.setParameter("idp", project.getId());
		
				
		if(userPerson.getId() != null){
			if(userPerson.getId() != -1){
				query.setParameter("up", userPerson.getId());
			}
		}
		
		if(userPerson.getId() != null){
			if(userPerson.getId() != -1){
				query.setParameter("tStatusId", taskStatus.getId());
			}
		}
		
		
		if(period.getStartDate() != null){
			query.setDate("sd",period.getStartDate());
				}
		
		if(period.getEndDate() != null){
			query.setDate("ed", period.getEndDate());
		}
	
		return (ArrayList<Task>) query.list();

	}

	@SuppressWarnings("unchecked")
	public List<Task> listTasksToDo(Long accountId) {
		Date fecha = new Date();
		
		System.out.println("Account id: " + accountId);
		System.out.println("Fecha: " + fecha);
		
		Query query = sessionFactory.getCurrentSession().createQuery("select t from Task as t join t.userTasks as ut where " +
				"ut.userPerson.account.id = :accountId and t.period.startDate < :todayDate and t.period.endDate > :todayDate");
		query.setParameter("accountId", accountId);
		query.setDate("todayDate", fecha);
		List<Task> list = query.list();
		System.out.println("LAS TAREAS SON:_");
		for (Task task : list) {
			System.out.println(task.getId());
		}
		 return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Task> listTasksByUser(Long accountId) {
		Query query = sessionFactory.getCurrentSession().createQuery("select t from Task as t join t.userTasks as ut where " +
				"ut.userPerson.account.id = :accountId");
		query.setParameter("accountId", accountId);
		return (List<Task>) query.list();
	}

	public long searchProjectId(long id) {
		Query query = sessionFactory.getCurrentSession().createQuery("select p.id from Project as p join p.tasks as t where t.id ="+id);
		return (Long) query.uniqueResult();
		
		
	}
	

}
